package com.yiboshi.rehrs.controller;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.yiboshi.rehrs.util.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.UUID;

/**
 * @author jack 2025/4/9
 */
@Slf4j
@RestController
@RequestMapping("")
//@Api(value = "/api/user/1000/android/5.1.0/",tags="A01.ApiInsideController",description = "A01.内部接口-管理档案首页接口")

public class ZhuShouTestController {


    String xiaofei_host = "http://weaw.zhenfenni.com/api/user/1000/android/";

    String da_host = "http://dx.matou.shop/api/user/1000/android/";


    //https://uyz.mmlwo.cn/api/user/1000/android/1.01076/zyltlogon
    //https://uyz.mmlwo.cn/api/user/1000/android/1.01073/zyltgnq
    String jinshe_host = "https://uyz.mmlwo.cn/api/user/1000/android/";
    @GetMapping("/")
    public String getRoot(@PathVariable String version, @PathVariable String path, @RequestBody JSONObject jsonObject) {
        return "";
    }


    @RequestMapping(value = "/js/api/user/1000/android/{version}/{path}", method = {RequestMethod.GET, RequestMethod.POST})
    public JSONObject jsPostDa(@PathVariable String version, @PathVariable String path, @RequestParam(value = "data",required = false) String data,@RequestParam(value = "sign",required = false) String sign) throws Exception {
        String url = xiaofei_host +version+"/"+ path;
        //String data = jsonObject.getString("data");
        String decrypt = "";
        boolean xiao = false;
        //xiaofeixia
        url = jinshe_host +version+"/"+ path;
        //decrypt = JinsheUtil.decode(data,JinsheUtil.rc4KeyNew);
        //decrypt = JsRSAUtils.publicDecrypt(data,JsRSAUtils.rsaKeyNew);
        xiao = false;
        //https://uyz.mmlwo.cn/api/user/1000/android/1.01073/logout 金蛇
        String decryptResStr = "";
        JSONObject jsonObject1 = null;

        Map<String, String> end = MapBuilder.start().end();
        end.put("Content-Type", "application/x-www-form-urlencoded");
        end.put("User-Agent", System.currentTimeMillis() / 1000 +"_"+ UUID.randomUUID().toString().replaceAll("-","") +" Dalvik/2.1.0 (Linux; U; Android 10; AQM-AL10 Build/HONORAQM-AL10)");

        HttpResponse httpResponse = HttpClientUtils.doPost(url,"", end,null,StrUtils.isBlank (data)?null:("data="+data+"&sign="+sign).getBytes());
        jsonObject1 = HttpClientUtils.toJson(httpResponse);
        String dataRes = jsonObject1.getString("data");
        if (StrUtil.isNotEmpty(dataRes)) {
            String decryptRes = JsRSAUtils.publicDecrypt(dataRes,JsRSAUtils.rsaKeyNew);
            decryptResStr = JSONUtil.toJsonPrettyStr(decryptRes);
        }
        jsonObject1.put("origin-req", decrypt);
        jsonObject1.put("origin-res", decryptResStr);
        log.info("==================================================================================================");
        log.info("url:{}", url);
        log.info("origin-req-data:{}", decrypt);
        log.info("origin-res-data:{}", decryptResStr);
        return jsonObject1;

    }


    @PostMapping("/api/user/1000/android/{version}/{path}")
    public JSONObject postDa(@PathVariable String version, @PathVariable String path, @RequestBody JSONObject jsonObject,@RequestParam(value = "data") String data,@RequestParam(value = "sign") String sign) {
        String url = xiaofei_host +version+"/"+ path;
        //String data = jsonObject.getString("data");
        String decrypt = "";
        boolean xiao = false;
        if(version.contains("5.2.7")){
            //大象
            url = da_host +version+"/"+ path;
            decrypt = AesElephantUtil.decrypt(data);
            xiao = true;
        }
        //http://weaw.zhenfenni.com/api/user/1000/android/5.1.8/logout
        else if(version.contains("5.2.1")){
            //xiaofeixia
            url = xiaofei_host +version+"/"+ path;
            decrypt = AesXiaofeixiaUtil.decrypt(data);
            xiao = true;
        }
        else if(version.contains("1.010")){
            //xiaofeixia
            url = jinshe_host +version+"/"+ path;
            //decrypt = JinsheUtil.decode(data,JinsheUtil.rc4KeyNew);
            decrypt = JsRSAUtils.publicDecrypt(data,JsRSAUtils.rsaKeyNew);
            xiao = false;
        }
        //https://uyz.mmlwo.cn/api/user/1000/android/1.01073/logout 金蛇
        String decryptResStr = "";
        JSONObject jsonObject1 = null;
        if(xiao){
            Map<String, String> end = MapBuilder.start("user-agent", "Mozilla/5.0 (Linux; Android 10; AQM-AL10 Build/HONORAQM-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/99.0.4844.88 Mobile Safari/537.36 uni-app Html5Plus/1.0 (Immersed/26.0)").end();
            jsonObject1 = HttpClientUtils.doJsonPost(url, end, jsonObject);
            String dataRes = jsonObject1.getString("data");
            if (StrUtil.isNotEmpty(dataRes)) {
                String decryptRes = AesElephantUtil.decrypt(dataRes);
                decryptResStr = JSONUtil.toJsonPrettyStr(decryptRes);
            }
        }else{
            Map<String, String> end = MapBuilder.start().end();
            end.put("Content-Type", "application/x-www-form-urlencoded");
            end.put("User-Agent", System.currentTimeMillis() / 1000 +"_"+ UUID.randomUUID().toString().replaceAll("-","") +" Dalvik/2.1.0 (Linux; U; Android 10; AQM-AL10 Build/HONORAQM-AL10)");

            jsonObject1 = HttpClientUtils.doJsonPost(url, end, jsonObject);
            String dataRes = jsonObject1.getString("data");
            if (StrUtil.isNotEmpty(dataRes)) {
                String decryptRes = JsRSAUtils.publicDecrypt(data,JsRSAUtils.rsaKeyNew);
                decryptResStr = JSONUtil.toJsonPrettyStr(decryptRes);
            }
        }
        jsonObject1.put("origin-req", decrypt);
        jsonObject1.put("origin-res", decryptResStr);
        log.info("==================================================================================================");
        log.info("url:{}", url);
        log.info("origin-req-data:{}", decrypt);
        log.info("origin-res-data:{}", decryptResStr);
        return jsonObject1;

    }


    public JSONObject postXiao(@PathVariable String path, @RequestBody JSONObject jsonObject) {
        String data = jsonObject.getString("data");
        String decrypt = AesXiaofeixiaUtil.decrypt(data);
        Map<String, String> end = MapBuilder.start("user-agent", "Mozilla/5.0 (Linux; Android 10; AQM-AL10 Build/HONORAQM-AL10; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/99.0.4844.88 Mobile Safari/537.36 uni-app Html5Plus/1.0 (Immersed/26.0)").end();
        String url = xiaofei_host + path;
        JSONObject jsonObject1 = HttpClientUtils.doJsonPost(url, end, jsonObject);
        String dataRes = jsonObject1.getString("data");
        String decryptResStr = "";
        if (StrUtil.isNotEmpty(dataRes)) {
            String decryptRes = AesXiaofeixiaUtil.decrypt(dataRes);
            decryptResStr = JSONUtil.toJsonPrettyStr(decryptRes);
        }
        jsonObject1.put("origin-req", decrypt);
        jsonObject1.put("origin-res", decryptResStr);
        log.info("==================================================================================================");
        log.info("url:{}", url);
        log.info("origin-req-data:{}", decrypt);
        log.info("origin-res-data:{}", decryptResStr);
        return jsonObject1;

    }

}
